﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using NetJsWire.Lite.Converters;
using NetJsWire.Lite.Designers;
using NetJsWire.Lite.Editors;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Form
{
    [Designer( typeof( WidgetDesigner ) )]
    [JsonObject( MemberSerialization = MemberSerialization.OptIn )]
    public class TextFieldLW : FieldLW
    {
        public TextFieldLW()
            : this( null )
        {
        }

        public TextFieldLW( IContainer container )
            : base( container )
        {
            base.Xtype = "textfield";
            Size = new Size( 121, 22 );
            AllowBlank = true;
        }

        [Browsable( false ), EditorBrowsable( EditorBrowsableState.Never ), DefaultValue( ( string ) null )]
        [JsonIgnore]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        //[DefaultValue( 121 )]
        [JsonProperty( "width" )]
        public override int Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
            }
        }

        [DefaultValue( 22 )]
        [JsonIgnore]
        public override int Height
        {
            get
            {
                return base.Height;
            }
            set
            {
                base.Height = value;
            }
        }

        [Browsable( true )]
        [JsonProperty( "allowBlank" )]
        [DefaultValue( true )]
        [Description( "Specify false to validate that the value's length is > 0 (defaults to true)" )]
        public bool AllowBlank { get; set; }

        [Browsable( true )]
        [JsonProperty( "blankText" )]
        [Description( "The error text to display if the allowBlank validation fails (defaults to 'This field is required')" )]
        [TypeConverter( typeof( StringConverter ) )]
        public string BlankText { get; set; }

        [Browsable( true )]
        [JsonProperty( "emptyText" )]
        [Description( "The default text to place into an empty field (defaults to null).\nNote: that this value will be submitted to the server if this field is enabled and configured with a name." )]
        public string EmptyText { get; set; }

        [Browsable( true )]
        [JsonProperty( "maxLengthText" )]
        [Description( "Error text to display if the maximum length validation fails (defaults to 'The maximum length for this field is {maxLength}')." )]
        public string MaxLengthText { get; set; }

        [Browsable( true )]
        [JsonProperty( "minLengthText" )]
        [Description( "Error text to display if the minimum length validation fails (defaults to 'The minimum length for this field is {minLength}')." )]
        public string MinLengthText { get; set; }

        [Browsable( true )]
        [JsonProperty( "maxLenght" )]
        [DefaultValue( 0 )]
        [Description( "Maximum input field length allowed by validation (leave at 0 to use default of int.Maximum). This behavior is intended to provide instant feedback to the user by improving usability to allow pasting and editing or overtyping and back tracking." )]
        public int MaxLength { get; set; }

        /// <summary>
        ///   <p>A custom validation function to be called during field validation ({@link #validateValue})
        ///     (defaults to <tt>null</tt>). If specified, this function will be called first, allowing the
        ///     developer to override the default validation process.</p>
        ///   <br><p>This function will be passed the following Parameters:</p>
        ///     <div class = "mdetail-params"><ul>
        ///                                     <li><code>value</code>: <i>Mixed</i>
        ///                                       <div class = "sub-desc">The current field value</div></li>
        ///                                   </ul></div>
        ///     <br><p>This function is to Return:</p>
        ///       <div class = "mdetail-params"><ul>
        ///                                       <li><code>true</code>: <i>Boolean</i>
        ///                                         <div class = "sub-desc"><code>true</code> if the value is valid</div></li>
        ///                                       <li><code>msg</code>: <i>String</i>
        ///                                         <div class = "sub-desc">An error message if the value is invalid</div></li>
        ///                                     </ul></div>
        /// </summary>
        [JsonProperty( "validator" )]
        [DefaultValue( ( string ) null )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [Editor( typeof( ScriptEditor ), typeof( UITypeEditor ) )]
        [Description( "A custom validation function to be called during field validation (validateValue) (defaults to null). If specified, this function will be called first, allowing the developer to override the default validation process." )]
        public string Validator { get; set; }


        public bool ShouldSerializeBlankText()
        {
            return !string.IsNullOrWhiteSpace( BlankText );
        }

        protected void ResetBlankText()
        {
            BlankText = null;
        }

        public bool ShouldSerializeEmptyText()
        {
            return !string.IsNullOrWhiteSpace( EmptyText );
        }

        protected void ResetEmptyText()
        {
            EmptyText = null;
        }

        public bool ShouldSerializeMaxLengthText()
        {
            return !string.IsNullOrWhiteSpace( MaxLengthText );
        }

        protected void ResetMaxLengthText()
        {
            MaxLengthText = null;
        }

        public bool ShouldSerializeMinLengthText()
        {
            return !string.IsNullOrWhiteSpace( MinLengthText );
        }

        protected void ResetMinLengthText()
        {
            MinLengthText = null;
        }

        protected override void OnPaint( PaintEventArgs e )
        {
            base.OnPaint( e );

            //Background and border
            using ( var br = new SolidBrush( Color.White ) )
            {
                e.Graphics.FillRectangle( br, ClientRectangle );
            }

            using ( var pen = new Pen( Color.FromArgb( 181, 184, 200 ) ) )
            {
                e.Graphics.DrawRectangle( pen, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1 );
            }
            var text = Value is string || ( Value != null && !string.IsNullOrWhiteSpace( Value.ToString() ) )
                           ? Value.ToString()
                           : null;
            if ( !string.IsNullOrWhiteSpace( text ) )
            {
                using ( var font = new Font( "Tahoma", 12, FontStyle.Regular, GraphicsUnit.Pixel ) )
                {
                    TextRenderer.DrawText( e.Graphics, text, font,
                                           new Rectangle( 3, 5, ClientSize.Width - 6, ClientSize.Height - 2 ),
                                           Color.Black, Color.White,
                                           TextFormatFlags.TextBoxControl );
                }
            }
        }

        public bool ShouldSerializeSize()
        {
            return Size.Width != 121 || Size.Height != 22;
        }

        protected void ResetSize()
        {
            Size = new Size( 121, 22 );
        }
    }
}
